import { createRouter, createWebHashHistory } from 'vue-router'
import LoginPage from '../components/LoginPage.vue'
import HomePage from '../components/HomePage.vue'
import MainPage from '../components/MainPage.vue'
import GoodsDettailPage from '../components/GoodsDetailPage.vue'
import ShopCar from '../components/ShopCar.vue'
import OrderPage from '../components/OrderPage.vue'
import SearchPage from '../components/SearchPage.vue'
import EvaluationPage from '../components/EvaluationPage.vue'
import Account from './AccountStore'
// 定义页面名称枚举
export enum PageName {
    login = 'login',
    home = 'home'
}
// 定义路由
const shopRouter = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/login',
            name: PageName.login,
            component: LoginPage
        },
        {
            path: '/home',
            name: PageName.home,
            component: HomePage,
            children: [
                {
                    path: 'main',
                    component: MainPage
                },
                {
                    path: 'goods/:id',
                    component: GoodsDettailPage,
                    name: 'goodsDetail',
                    props: true
                },{
                    path: 'shop',
                    component: ShopCar,
                    name: 'shopCar'
                },{
                    path: 'order',
                    component: OrderPage,
                    name: 'order'
                },{
                    path: 'search/:keyword',
                    component: SearchPage,
                    name: 'search',
                    props: true
                },{
                    path: 'evaluation/:order',
                    component: EvaluationPage,
                    name: 'evaluation',
                    props: true
                }
            ],
            redirect:'/home/main'
        }
    ]
});
// 定义前置守卫
shopRouter.beforeEach((to)=>{
    // 如果用户未登录，且访问了非登录页，则重定向到登录页
    if ((to.name != PageName.login) && !Account().isLogin) {
        return {
            name: PageName.login
        }
    }
})
// 导出路由
export default shopRouter;